package com.ruoyi.web.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.storage.domain.RepoCk;
import com.ruoyi.storage.domain.RepoRk;
import com.ruoyi.storage.domain.RepoWare;
import com.ruoyi.storage.service.RepoCkService;
import com.ruoyi.storage.service.RepoRkService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/storage/rk")
public class RKManageController extends BaseController {
    @Autowired
    private RepoRkService repoRkService;
    @Autowired
    private RepoCkService repoCkService;

    private String prefix = "/storage/rk";

    @RequiresPermissions("storage:rk:view")
    @RequestMapping("")
    public String index(){
        return prefix + "/rk_index";
    }

    /**
     * 查看入库列表
     * @return
     */
    @RequiresPermissions("storage:rk:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(RepoRk repoRk)
    {
        startPage();
        List<RepoRk> list = repoRkService.selectAllRkList(repoRk);
        return getDataTable(list);
    }


    /**
     * 展示信息
     */
    @RequiresPermissions("storage:rk:detail")
    @GetMapping("/detail/{bid}")
    public String view(@PathVariable("bid") Integer id, ModelMap mmap)
    {
        RepoRk repoRk = new RepoRk();
        repoRk.setRkId(id);
        List<RepoRk> list = repoRkService.selectAllRkList(repoRk);
        mmap.put("repoRk", list.get(0));
        return prefix + "/view";
    }


    /**
     * 修改信息
     */
    @RequiresPermissions("storage:rk:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(RepoRk repoRk,ModelMap mmap)
    {
        List<RepoRk> repoRks = repoRkService.listByIds(Arrays.asList(repoRk.getRkId()));
        String status = repoRks.get(0).getStatus();
        if ("1".equals(status)){
            repoRk.setStatus("0");
        }
        if ("0".equals(status)){
            repoRk.setStatus("1");
        }
        return toAjax(repoRkService.updateRk(repoRk));
    }

    /**
     * 修改信息
     */
    @RequestMapping("/edit/{bid}")
    public String edits(@PathVariable("bid") Integer id,ModelMap mmap)
    {
        mmap.addAttribute("rkId",id);
        return prefix + "/edit_html";
    }


    /**
     * 添加入库信息实现跳转
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<String> list = repoRkService.getAllPoPurIndent();
        List<RepoWare> repoWares = repoCkService.getAllWare();
        SysUser sysUser = getSysUser();
        model.addAttribute("list",list);
        model.addAttribute("repoWares",repoWares);
        model.addAttribute("sysUser",sysUser);
        return prefix + "/add";
    }

    /**
     * 将入库信息保存到数据库
     */
    @RequiresPermissions("storage:rk:add")
    @Log(title = "入库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RepoRk repoRk)
    {
        repoRk.setRkTime(new Date());
        repoRk.setSysRkUserId(getUserId().intValue());
        repoRk.setStatus("0");
        return toAjax(repoRkService.save(repoRk));
    }


}
